require 'login_system'
class Plugin::WeatherController < ActionController::Base
  before_filter :set_site_template_root
  def set_site_template_root
    self.class.template_root = "#{RAILS_ROOT}/components/"
    @template.base_path = template_root
  end 
  require 'uri'
  require 'net/http'
  require 'rexml/document'
  include LoginSystem
  before_filter :login_required, :only => [:config]
  def show
    @plugin=Extplugin.find_by_name("weather")
	@config=@plugin.config 
  	if @plugin.activated==1
  	  render(:layout => false)
  	else
  	  render :text => _("not activated")
  	end  	
  end
	
  def config
    @plugin=Extplugin.find_by_name("weather")
    @config=@plugin.config 
  end

  def fetch
   @url="http://xoap.weather.com/search/search?where="+params["city"].gsub(/\s/, '').to_s
   if session[:loadedsettings]["proxy_use"]==1
     r=Net::HTTP::Proxy(session[:loadedsettings]["proxy_addr"], session[:loadedsettings]["proxy_port"]).get_response URI.parse(@url)
   else
     r=Net::HTTP.get_response URI.parse(@url)
   end
   if r
    doc=REXML::Document.new r.body
    codedcity=""
    @string=Array.new
    doc.elements.each("/search/loc") {|element| 
    codedcity=element.attributes["id"].to_s
    @url2="http://xoap.weather.com/weather/local/"+codedcity+"?cc=*%E2%88%8F=xoap&par=xxx&key=xxx&unit="+params["measurement"].to_s
    if session[:loadedsettings]["proxy_use"]==1
     r2=Net::HTTP::Proxy(session[:loadedsettings]["proxy_addr"], session[:loadedsettings]["proxy_port"]).get_response URI.parse(@url2)
   else
     r2=Net::HTTP.get_response URI.parse(@url2)
   end
    weather=REXML::Document.new r2.body
    dnam=weather.elements.to_a("//dnam")
    temperature=weather.elements.to_a("//tmp")
    wtype=weather.elements.to_a("//cc/t")
    @string+=["code"=>codedcity.to_s,"name"=>dnam.to_s,"temp"=>temperature.to_s,"type"=>wtype.to_s]
  }
   end
  end

end